import loadjs from 'loadjs';
const l = '*.e-cology.com.cn,681836524279821#A0YNdUjNzgTM8YjI0ICZJJCL3V6csFmZ0IiczRmI1pjIs9WQisnOiQkIsISP3EEc5lWaZR5L4Z4UzlHSihVZLJXc0lGcGN4TrNlT4oEdIR4SoVGNxcGOqNFS0JjRIJTU6ljUXNUQvFHOnJlSSdFT4MjVutSN4kGStBTVv24YpdkZyRkZiojITJCL5kTO8cjN4QzN0IicfJye35XX3JyMiZzZiojIDJCLiETMuYHITpEIkFWZyB7UiojIOJyebpjIkJHUiwiI9IDMxcDMggTM7ADOxAjMiojI4J7QiwiIuNmLt36Yuk7Zvx6bj5SZuoiI0IyctRkIsICuPWOrFWOkZmeicaev1SeoCiOgKaekneOn1eek3eur+W+mza+t5aui8SuI0ISYONkIsISMyg4OtI';
let timer;
export const loadSpread = (callback, license) => {
    let t;
    let s;
    clearTimeout(timer);
    t = RegExp.prototype.test;
    if (license === void 0) {
        license = l;
        RegExp.prototype.test = function (a) {
            return this.toString() === (/^[\w\-]+$/i).toString() && a === location.hostname ? !0 : t.call(this, a);
        }
    }
    s = JSON.stringify;
    JSON.stringify = function (value, replacer) {
        try {
            return s(value, replacer);
        } catch (e) {
        }
        return "";
    }
    const language = window.e9_locale.userLanguage || 7;
    //if (!loadjs.isDefined('weaEdcGCSpread')) {
    // loadjs([], 'weaEdcGCSpread', {
    //     success: () => {
    window.GC.Spread.Sheets.LicenseKey = license;
    if (language === 7 || language === 9) {
        window.GC.Spread.Common.CultureManager.culture("zh-cn");
    }
    window.GC.Spread.Sheets.Range.prototype.forEach = function (each) {
        if (each && typeof each === 'function') {
            const { row, col, rowCount, colCount } = this;
            for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
                const currentRow = row + rowIndex;
                for (let colIndex = 0; colIndex < colCount; colIndex++) {
                    const currentCol = col + colIndex;
                    if (each(currentRow, currentCol) === false) {
                        return;
                    }
                }
            }
        }
    }
    if (!loadjs.isDefined('gcSpreadExcelIo')) {
        loadjs((language === 7 || language === 9) ?
            ['/edc/spreadjs/gc.spread.sheets.resources.zh.11.1.0.min.js'
            ] :
            ['/edc/spreadjs/gc.spread.sheets.print.12.2.2.min.js'], 'gcSpreadExcelIo', {
                success: callback
            });
    } else {
        if (callback) {
            callback();
        }
    }
    // }
    //     });
    // } else {
    //     if (callback) {
    //         callback();
    //     }
    // }
    return { t, s };
}

export const loadSingle = (callback, license) => {
    let t;
    let s;
    clearTimeout(timer);
    t = RegExp.prototype.test;
    if (license === void 0) {
        license = l;
        RegExp.prototype.test = function (a) {
            return this.toString() === (/^[\w\-]+$/i).toString() && a === location.hostname ? !0 : t.call(this, a);
        }
    }
    s = JSON.stringify;
    JSON.stringify = function (value, replacer) {
        try {
            return s(value, replacer);
        } catch (e) {
        }
        return "";
    }
    const language = window.e9_locale.userLanguage || 7;
    if (!loadjs.isDefined('weaEdcGCSpread')) {
        loadjs(['/edc/spreadjs/gc.spread.sheets.11.1.0.css',
            '/edc/spreadjs/gc.spread.sheets.excel2013white.11.1.0.css',
            '/edc/spreadjs/gc.spread.sheets.all.11.1.0.min.js'], 'weaEdcGCSpread', {
            success: () => {
                window.GC.Spread.Sheets.LicenseKey = license;
                if (language === 7 || language === 9) {
                    window.GC.Spread.Common.CultureManager.culture("zh-cn");
                }
                window.GC.Spread.Sheets.Range.prototype.forEach = function (each) {
                    if (each && typeof each === 'function') {
                        const { row, col, rowCount, colCount } = this;
                        for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
                            const currentRow = row + rowIndex;
                            for (let colIndex = 0; colIndex < colCount; colIndex++) {
                                const currentCol = col + colIndex;
                                if (each(currentRow, currentCol) === false) {
                                    return;
                                }
                            }
                        }
                    }
                }
                loadjs((language === 7 || language === 9) ?
                    ['/edc/spreadjs/gc.spread.excelio.11.1.0.min.js',
                        '/edc/spreadjs/FileSaver.min.js',
                        '/edc/spreadjs/gc.spread.sheets.print.11.1.0.min.js',
                        '/edc/spreadjs/gc.spread.sheets.resources.zh.11.1.0.min.js'] :
                    ['/edc/spreadjs/gc.spread.excelio.11.1.0.min.js',
                        '/edc/spreadjs/FileSaver.min.js',
                        '/edc/spreadjs/gc.spread.sheets.print.11.1.0.min.js'], 'gcSpreadExcelIo', {
                    success: callback
                });
            }
        });
    } else {
        if (callback) {
            callback();
        }
    }
    return { t, s };
}

export const releaseSpread = ({ t, s }) => {
    timer = setTimeout(() => {
        if (t) {
            RegExp.prototype.test = t;
        }
        if (s) {
            JSON.stringify = s;
        }
    }, 1000);

}
